# TODO 1.导入pymysql
import pymysql

# TODO 2.创建连接对象
conn = pymysql.connect(host='localhost', user='root', password='root', database='advanced', port=3306, charset='utf8')
# TODO 3.获取游标对象
cursor = conn.cursor()
# TODO 4.执行sql语句
# TODO 5.提交或者回滚
try:
    sql = "INSERT INTO student(stu_id, stu_no, stu_name) VALUES(8, 'itsrc-016', '许嵩')"
    rows = cursor.execute(sql)  # 返回受影响行数
    print('受影响行数%d' % rows)
except Exception as e:
    # 注意: 提交或者回滚使用的是数据库连接对象
    # 5.1 有异常, 回滚
    print(e)
    conn.rollback()    # rollback回滚
else:
    # 5.2 没有异常, 提交
    conn.commit()    # commit 提交 

# TODO 6.关闭游标对象
cursor.close()
# TODO 7.关闭连接对象
conn.close()
